﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.PM;
using dz.u9.webapi.u9schema;
using dz.util.core;
using Microsoft.AspNetCore.Mvc;

namespace dz.u9.webapi.Controllers
{
    /// <summary>
    /// 供应商价表
    /// </summary>

    [ApiController]
    [Route("api/[controller]/[action]")]
    public class SupplierPriceController : U9BaseController
    {
        [HttpPost]
        public async Task<U9CResult> Add(SupplierPrice dto)
        {
            if (IsFirst(dto))
            {
                return await U9Invoke(JsonUtil.Serialize(GetPriceListDto(dto)), new ApiParm() { Url = "DzSalePriceList/Add", Desc = "销售价表新增" });
            }
            else
            {
                return await U9Invoke(JsonUtil.Serialize(GetPriceAdjust(dto)), new ApiParm() { Url = "DzPurPriceAdjustment/AddAndAudit", Desc = "厂商价格调整新增审核" });
            }
        }

        private DzPurPriceAdjustmentDTO GetPriceAdjust(SupplierPrice dto)
        {
            DzPurPriceAdjustmentDTO rtn = new DzPurPriceAdjustmentDTO();

            rtn.DocNo = "aaa";
            rtn.PriceList = GetPriceList(dto);            
            rtn.Supplier = dto.Code;
            rtn.Org = dto.Org;

            List<DzPurPriceAdjustLineDTO> lines = new List<DzPurPriceAdjustLineDTO>();

            foreach (var item in dto.SupplierPriceLines)
            {
                DzPurPriceAdjustLineDTO line = new DzPurPriceAdjustLineDTO();

                line.ItemInfo = item.ItemInfo;
                line.Uom = item.Uom;
                line.FromDate = DateTime.Now.Date;
                line.NewPrice = item.Price;
                line.DescFlexField = item.DescFlexField;
                

                lines.Add(line);
            }

            rtn.PurPriceLines = lines;

            return rtn;
        }

        private string GetPriceList(SupplierPrice dto)
        {
            string sql = @$"select top 1 a.code from PPR_PurPriceList a
                            inner join CBO_Supplier b on a.Supplier = b.id
                            inner join Base_Organization c on c.id = a.Org
                            where b.Code='{dto.Code}' and c.code = '{dto.Org}'";

            return utils.ExecuteScalar<string>(sql).Result;

        }

        private DzPurPriceListDTO GetPriceListDto(SupplierPrice dto)
        {
            DzPurPriceListDTO rtn = new DzPurPriceListDTO();

            rtn.Code = dto.Code;
            rtn.Currency = "C001";
            rtn.Supplier = dto.Code;

            List<DzPurPriceLineDTO> lines = new List<DzPurPriceLineDTO>();

            foreach (var item in dto.SupplierPriceLines)
            {
                DzPurPriceLineDTO line = new DzPurPriceLineDTO();

                line.Price = item.Price;                
                line.Uom = item.Uom;
                line.ItemInfo = item.ItemInfo;
                line.DescFlexField = item.DescFlexField;
                line.FromDate = DateTime.Now.Date;
                lines.Add(line);
            }


            rtn.PurPriceLines = lines;

            return rtn;
        }

        private bool IsFirst(SupplierPrice dto)
        {
            string sql = @$"select count(1) from PPR_PurPriceList a
                            inner join CBO_Supplier b on a.Supplier = b.id
                            inner join Base_Organization c on c.id = a.Org
                            where b.Code='{dto.Code}' and c.code = '{dto.Org}'";

            int r = utils.ExecuteScalar<int>(sql).Result;

            return r == 0;
        }
    }
}

